package com.cup.bddm.api.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cup.bddm.api.entity.MerchantEntity;
import com.cup.bddm.api.req.MerchantVo;
import com.cup.bddm.api.req.StreetVo;
import com.cup.bddm.api.res.ResVo;
import com.cup.bddm.api.service.MerchantService;
import com.cup.bddm.api.service.StreetService;
import com.cup.bddm.config.Config;
import com.cup.bddm.handler.Token;

/**
 * 商户
 * 查找、修改、删除，考虑总公司和分公司；
 * 所以：公司编号必须有；传到sql层是作为条件，而不是参数；
 */
@Controller
@RequestMapping(value = "/api/merchant", method = RequestMethod.POST)
public class MerchantController extends BaseController {
	static Logger logger = Logger.getLogger(MerchantController.class);

	@Autowired
	StreetService streetService;

	@Autowired
	MerchantService merchantService;

	/**
	 * 添加
	 */
	@Token
	@RequestMapping(value = "/add")
	@ResponseBody
	public ResVo add(@RequestBody MerchantVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
		if (StringUtils.isEmpty(vo.getCup_branch_ins_id_cd())) {
			return the().cup_branch_ins_id_cd().is_empty().vo();
		}
		if (!Config.CUP_INS_ID.equals(vo.getCup_branch_ins_id_cd()) && !vo.getCup_branch_ins_id_cd().equals(user().getCup_branch_ins_id_cd())) {
			return the().cup_branch_ins_id_cd().is_invalid().vo();
		}
		if (StringUtils.isEmpty(vo.getStreet_cd())) {
			return the().street_cd().is_empty().vo();
		}
		if (StringUtils.isEmpty(vo.getMchnt_cd())) {
			return the().mchnt_cd().is_empty().vo();
		}
		if (StringUtils.isEmpty(vo.getTerm_id())) {
			return the().term_id().is_empty().vo();
		}
		StreetVo temp = new StreetVo();
		temp.setCup_branch_ins_id_cd(vo.getCup_branch_ins_id_cd());
		temp.setStreet_cd(vo.getStreet_cd());
		if (streetService.oneByStreetCd(temp, true) == null) {
			return the().Street().do_not_exist().vo();
		}
		MerchantEntity merchant = merchantService.oneByMchntCdAndTermId(vo, true);
		if (merchant != null) {
			return the().Merchant().Term().already_exist().eg(merchant.getStore_nm() + " " + merchant.getTerm_id()).vo();
		}
		merchantService.add(vo);
		return ResVo.ok();
	}

	/**
	 * 批量添加
	 */
	@Token
	@RequestMapping(value = "/add_batch")
	@ResponseBody
	public ResVo addBatch(@RequestBody List<MerchantVo> list) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
		int ok = 0, up = 0;
		StringBuilder m = new StringBuilder();
		for (int i = 1; i <= list.size(); i++) {
			MerchantVo vo = list.get(i - 1);
			if (StringUtils.isEmpty(vo.getCup_branch_ins_id_cd())) {
				m.append(i + ":cup_branch_ins_id_cd is empty");
				continue;
			}
			if (!Config.CUP_INS_ID.equals(vo.getCup_branch_ins_id_cd()) && !vo.getCup_branch_ins_id_cd().equals(user().getCup_branch_ins_id_cd())) {
				m.append(i + ":cup_branch_ins_id_cd is invalid\n");
				continue;
			}
			if (StringUtils.isEmpty(vo.getStreet_cd())) {
				m.append(i + ":street_cd is empty");
				continue;
			}
			if (StringUtils.isEmpty(vo.getMchnt_cd())) {
				m.append(i + ":mchnt_cd is empty\n");
				continue;
			}
			StreetVo temp = new StreetVo();
			temp.setCup_branch_ins_id_cd(vo.getCup_branch_ins_id_cd());
			temp.setStreet_cd(vo.getStreet_cd());
			if (streetService.oneByStreetCd(temp, true) == null) {
				m.append(i + ":Street do not exist, " + vo.getStreet_cd() + "\n");
				continue;
			}
			MerchantEntity merchant = merchantService.oneByMchntCdAndTermId(vo, true);
			if (merchant != null) {
				if (merchantService.modify(merchant, vo) > 0) {
					m.append(i + ":update ok\n");
					up++;
				}
			} else {
				if (merchantService.add(vo) > 0) {
					m.append(i + ":add ok\n");
					ok++;
				}
			}
		}
		return ResVo.batch(ok, up, list.size() - ok - up, m.toString());
	}

	/**
	 * 查找-按名字
	 */
	@Token
	@RequestMapping(value = "/find")
	@ResponseBody
	public ResVo find(@RequestBody MerchantVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
//		if (vo.invalidIns()) {
//			return the().cup_branch_ins_id_cd().is_empty().vo();
//		}
		vo.resetVoIns(user());
		return ResVo.data(merchantService.findByVo(vo));
	}

	/**
	 * 修改
	 */
	@Token
	@RequestMapping(value = "/modify")
	@ResponseBody
	public ResVo modify(@RequestBody MerchantVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
//		if (vo.invalidIns()) {
//			return the().cup_branch_ins_id_cd().is_empty().vo();
//		}
		vo.resetVoIns(user());
		if (vo.invalidId()) {
			return the().id().is_invalid().vo();
		}
		if (StringUtils.isEmpty(vo.getMchnt_cd())) {
			return the().mchnt_cd().is_empty().vo();
		}
		MerchantEntity entity = merchantService.oneById(vo, true);
		if (entity == null) {
			return the().Merchant().do_not_exist().vo();
		}
		merchantService.modify(entity, vo);
		return ResVo.ok();
	}

	/**
	 * 删除-假删除
	 */
	@Token
	@RequestMapping(value = "/delete")
	@ResponseBody
	public ResVo delete(@RequestBody MerchantVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
//		if (vo.invalidIns()) {
//			return the().cup_branch_ins_id_cd().is_empty().vo();
//		}
		vo.resetVoIns(user());
		if (vo.invalidId()) {
			return the().id().is_invalid().vo();
		}
		MerchantEntity entity = merchantService.oneById(vo, true);
		if (entity == null) {
			return the().Merchant().do_not_exist().vo();
		}
		merchantService.setIsDel(entity);
		return ResVo.ok();
	}

	/**
	 * 删除-真删除
	 */
	@Token
	@RequestMapping(value = "/delete_ever")
	@ResponseBody
	public ResVo delete_ever(@RequestBody MerchantVo vo) {
//		if (invalidToken(vo)) {
//			return the().token_err(token_err).vo();
//		}
//		if (vo.invalidIns()) {
//			return the().cup_branch_ins_id_cd().is_empty().vo();
//		}
		vo.resetVoIns(user());
		if (vo.invalidId()) {
			return the().id().is_invalid().vo();
		}
		MerchantEntity entity = merchantService.oneById(vo, false);
		if (entity == null) {
			return the().Merchant().do_not_exist().vo();
		}
		merchantService.delete(entity);
		return ResVo.ok();
	}
}
